 /**
 * v-hasPermi 操作权限处理
 * Copyright (c) 2019 ruoyi
 */

import store from '@/store'

export default {
  inserted(el, binding, vnode) {
    const { value } = binding
    const all_permission = "*:*:*";
    let permissions = store.getters && store.getters.permissions
// 如果 Vuex Store 未初始化或没有角色信息，从 localStorage 获取
    if (!permissions) {
      const user = JSON.parse(localStorage.getItem('user'));
      if (user && user.role) {
        permissions = user.role;
      } else {
        // 如果 localStorage 也没有角色信息，设置一个默认值或处理错误
        console.error('Unable to retrieve user roles from both Vuex and localStorage.');
        permissions = []; // 或者其他默认值
      }
    }
    if (value && value instanceof Array && value.length > 0) {
      const permissionFlag = value

      const hasPermissions = permissions.some(permission => {
        return all_permission === permission || permissionFlag.includes(permission)
      })

      if (!hasPermissions) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error(`请设置操作权限标签值`)
    }
  }
}
